package jmine.tec.di.resolver.impl;

import jmine.tec.di.scope.TypedScope;
import jmine.tec.di.type.TypeConverter;
import jmine.tec.di.type.TypeConvertionException;
import jmine.tec.di.wrapper.Wrapper;

/**
 * @author takeshi
 */
public class SuffixOfObjectResolver extends AbstractManyObjectsResolver {

    private final String preffix;

    private final int len;

    /**
     * C'tor
     * 
     * @param desiredType class
     * @param typeConverter Converter
     * @param preffix {@link String}
     * @param keyType class
     * @param keyTypeConverter type converter
     */
    public SuffixOfObjectResolver(Class<?> desiredType, TypeConverter<?> typeConverter, final String preffix, Class<?> keyType,
            TypeConverter<?> keyTypeConverter) {
        super(desiredType, typeConverter, keyType, keyTypeConverter);
        this.preffix = preffix;
        this.len = preffix.length();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean matches(String name) {
        return name.startsWith(this.preffix);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object resolveObject(TypedScope properties, Wrapper wrapper, Class<?> requiredComponentClass) throws TypeConvertionException {
        String[] collectAllMatching = this.collectAllMatching(properties);
        Object[] values = this.collectValuesConvertType(properties, collectAllMatching);
        for (int i = 0; i < collectAllMatching.length; i++) {
            collectAllMatching[i] = collectAllMatching[i].substring(this.len);
        }
        return wrapper.wrap(collectAllMatching, values, requiredComponentClass);
    }

}
